#include "lib/web.cpp"
#include "lib/svg.cpp"
#include "_同学录.cpp"

string _2str(int d) {
	stringstream ss;
	ss << d;
	return ss.str();
}

int _2int(const string & s) {
	stringstream ss(s);
	int d;
	ss >> d;
	return d;
}

// 带框的文字
class text_with_box : public SVG_Object
{
public:
	int x;
	int y;
	int width;
	int height;
	text tex;
	rect box;

	text_with_box(string tex, int x, int y, int width, int height) :
				x(x), y(y), width(width), height(height) {
		this->tex.x = _2str(x+height/2);
		this->tex.y = _2str(y+height*2/3);
		this->tex.font_size = _2str(height/2);
		this->tex.stroke_width = _2str(1);
		this->tex.content = tex;
		box.x = _2str(x);
		box.y = _2str(y);
		box.width = _2str(width);
		box.height = _2str(height);
		box.stroke = "black";
		box.stroke_width = _2str(1);
	}
	string html() const {
		return box.html()+tex.html();
	}
};

// 按钮
class Button : public text_with_box {
public:
	Button(string tex, int x, int y, int width, int height) :
			text_with_box(tex, x, y, width, height) {
		this->tex.fill = "white";
		box.rx = _2str(height/5);
		box.ry = _2str(height/5);
		box.fill = "#333366";
		box.stroke_width = _2str(0);
	}
	bool contains(int x, int y) {
		x -= 10;
		y -= 10;
		return x>=this->x and y>=this->y and
			x<=this->x+this->width and y<=this->y+this->height;
	}
};

// 表格
class Table : public SVG_Object {
public:
	int max_x;
	int max_y;
	string _html;
	Table(string title, int x, int y, 
			InfoSys & s, const vector<int> & indexes) {
		stringstream ss;
		int cell_height = 28;
		int cell_width = 100;
		string head_color = "#505050";
		string border_color = "#C0C0C0";
		string cell_color1 = "#F8F8F8";
		string cell_color2 = "#F0F0F0";
		// 标题
		text_with_box title_(title, x, y, 130, 46);
		title_.box.fill = "none";
		title_.box.stroke_width = _2str(0);
		title_.tex.fill = head_color;
		ss << title_.html();
		// 表头
		text_with_box header1("姓名", x, y+50, cell_width, cell_height);
		header1.box.fill = head_color;
		header1.box.stroke = border_color;
		header1.tex.fill = cell_color2;
		ss << header1.html();
		text_with_box header2("学号", x+cell_width, y+50, cell_width, cell_height);
		header2.box.fill = head_color;
		header2.box.stroke = border_color;
		header2.tex.fill = cell_color2;
		ss << header2.html();
		max_x = 2 * cell_width;
		// 表格内容
		I(i, indexes.size()) {
			Record & r = s.records[indexes[i]];
			text_with_box cell1(r.name, x, y+50+cell_height*(i+1), cell_width, cell_height);
			cell1.box.fill = (i%2==0 ? cell_color1 : cell_color2);
			cell1.box.stroke = border_color;
			cell1.tex.fill = head_color;
			ss << cell1.html();
			text_with_box cell2(r.id, x+cell_width, y+50+cell_height*(i+1), cell_width, cell_height);
			cell2.box.fill = (i%2==0 ? cell_color1 : cell_color2);
			cell2.box.stroke = border_color;
			cell2.tex.fill = head_color;
			ss << cell2.html();
			max_y = y+50+cell_height*(i+2);
		}
		_html = ss.str();
	}
	string html() const {
		return _html;
	}
};

int main()
{
	text title;
	title.content = "同学录";
	title.x = "165";
	title.y = "50";
	title.font_size = "30";

	Button b1("添加同学", 10, 70, 95, 30);
	Button b2("删除同学", 110, 70, 95, 30);
	Button b3("更新同学", 210, 70, 95, 30);
	Button b4("查找同学", 310, 70, 95, 30);

	SVG svg;
	svg += title;
	svg += b1;
	svg += b2;
	svg += b3;
	svg += b4;

	// 处理按钮事件
	data["mouse_request"] = "true";
	if (data.has("mouse_x")) {
		int mouse_x = _2int(data["mouse_x"]);
		int mouse_y = _2int(data["mouse_y"]);
		if (b1.contains(mouse_x, mouse_y))
			data["action"] = "add_record";
		else if (b2.contains(mouse_x, mouse_y))
			data["action"] = "remove_record";
		else if (b3.contains(mouse_x, mouse_y))
			data["action"] = "update_record";
		else if (b4.contains(mouse_x, mouse_y))
			data["action"] = "search_record";
		else
			data.del("action");
		data.del("mouse_x");
		data.del("mouse_y");
		data.del("step");
	}

	// 处理添加
	if (data["action"] == "add_record") {
		if (not data.has("step")) {
			data["step"] = "1";
		}
		if (data["step"] == "1") {
			data["prompt_request"] = "请输入待添加同学的名字";
			data["step"] = "2";
		}
		else if (data["step"] == "2") {
			if (data.has("prompt_response") and 
					data["prompt_response"]!="") {
				data["name"] = data["prompt_response"];
				data["prompt_request"] = "请输入待添加同学的学号";
				data["step"] = "3";
			}
			else
				data.del("action");
		}
		else if (data["step"] == "3") {
			if (data.has("prompt_response") and 
					data["prompt_response"]!="") {
				data["step"] = "4";
				InfoSys s;
				Record r;
				r.name = data["name"];
				r.id = data["prompt_response"];
				s.records.push_back(r);
				int n = s.records.size();
				vector<int> indexes = {n-1};
				Table table("添加结果", 10, 120, s, indexes);
				svg += table;
			}
			data.del("action");
		}		
	}

	// 处理查找
	else if (data["action"] == "search_record") {
		if (not data.has("step")) {
			data["step"] = "1";
		}
		if (data["step"] == "1") {
			data["prompt_request"] = "请输入要搜索的学号";
			data["step"] = "2";
		}
		else if (data["step"] == "2") {
			if (data.has("prompt_response") and 
					data["prompt_response"]!="") {
				string id = data["prompt_response"];
				InfoSys s;
				vector<int> indexes = s.find("学号", id);
				Table table("搜索结果", 10, 120, s, indexes);
				svg += table;
			}
			data.del("action");
		}
	}

	// 处理更新
	else if (data["action"] == "update_record") {
		
	}
		
	// 处理删除
	else if (data["action"] == "remove_record") {
	}

	else {
		InfoSys s;
		vector<int> indexes;
		I(i, s.records.size()) indexes.push_back(i);
		Table table("所有数据", 10, 120, s, indexes);
		svg += table;
		svg.width = _2str(max(table.max_x, 800));
		svg.height = _2str(max(table.max_y, 600));
	}

	data.del("stderr");
	cerr << data.encode_json() << endl;
	cout << svg;
}